﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VietImage.Models;
using VietImage.Supports;
using VietImage.Logs;

namespace VietImage.Roles
{
    public class OwnerRole : AdministratorRole
    {
        #region Methods
        public List<Customer> ViewCustomerInformation()
        {
            return Customer.GetListAvailable();
        }

        public SortedDictionary<System.Object, int> ViewUserAction()
        {
            SortedDictionary<System.Object, int> list = new SortedDictionary<System.Object, int>(new LogDateComparer());

            foreach (LogCategory log in LogCategory.GetList())
                list.Add(log, 1);

            foreach (LogCustomer log in LogCustomer.GetList())
                list.Add(log, 2);

            foreach (LogEmail log in LogEmail.GetList())
                list.Add(log, 3);

            foreach (LogImage log in LogImage.GetList())
                list.Add(log, 4);

            foreach (LogInfo log in LogInfo.GetList())
                list.Add(log, 5);

            foreach (LogImageCategory log in LogImageCategory.GetList())
                list.Add(log, 6);

            foreach (LogImageLightbox log in LogImageLightbox.GetList())
                list.Add(log, 7);

            foreach (LogLightbox log in LogLightbox.GetList())
                list.Add(log, 8);

            foreach (LogNoticeEmail log in LogNoticeEmail.GetList())
                list.Add(log, 9);

            foreach (LogPayment log in LogPayment.GetList())
                list.Add(log, 10);

            foreach (LogRecentActivity log in LogRecentActivity.GetList())
                list.Add(log, 11);

            foreach (LogSubscriber log in LogSubscriber.GetList())
                list.Add(log, 12);

            foreach (LogTranslation log in LogTranslation.GetList())
                list.Add(log, 13);

            foreach (LogUser log in LogUser.GetList())
                list.Add(log, 14);

            return list;
        }

        public List<ErrorItem> Rollback(System.Object ob, int kind)
        {
            List<ErrorItem> errorList = new List<ErrorItem>();

            switch (kind)
            {
                case 1:
                    {
                        LogCategory log = (LogCategory)ob;
                        errorList = log.Rollback(this.UserInfo.ID, this.ID_Language);
                        break;
                    }
                case 2:
                    {
                        LogCustomer log = (LogCustomer)ob;
                        errorList = log.Rollback(this.UserInfo.ID, this.ID_Language);
                        break;
                    }
                case 3:
                    {
                        LogEmail log = (LogEmail)ob;
                        errorList = log.Rollback(this.UserInfo.ID, this.ID_Language);
                        break;
                    }
                case 4:
                    {
                        LogImage log = (LogImage)ob;
                        errorList = log.Rollback(this.UserInfo.ID, this.ID_Language);
                        break;
                    }
                case 5:
                    {
                        LogImageCategory log = (LogImageCategory)ob;
                        errorList = log.Rollback(this.UserInfo.ID, this.ID_Language);
                        break;
                    }
                case 6:
                    {
                        LogImageLightbox log = (LogImageLightbox)ob;
                        errorList = log.Rollback(this.UserInfo.ID, this.ID_Language);
                        break;
                    }
                case 7:
                    {
                        LogInfo log = (LogInfo)ob;
                        errorList = log.Rollback(this.UserInfo.ID, this.ID_Language);
                        break;
                    }
                case 8:
                    {
                        LogLightbox log = (LogLightbox)ob;
                        errorList = log.Rollback(this.UserInfo.ID, this.ID_Language);
                        break;
                    }
                case 9:
                    {
                        LogNoticeEmail log = (LogNoticeEmail)ob;
                        errorList = log.Rollback(this.UserInfo.ID, this.ID_Language);
                        break;
                    }
                case 10:
                    {
                        LogPayment log = (LogPayment)ob;
                        errorList = log.Rollback(this.UserInfo.ID, this.ID_Language);
                        break;
                    }
                case 11:
                    {
                        break;
                    }
                case 12:
                    {
                        LogSubscriber log = (LogSubscriber)ob;
                        errorList = log.Rollback(this.UserInfo.ID, this.ID_Language);
                        break;
                    }
                case 13:
                    {
                        LogTranslation log = (LogTranslation)ob;
                        errorList = log.Rollback(this.UserInfo.ID, this.ID_Language);
                        break;
                    }
                case 14:
                    {
                        LogUser log = (LogUser)ob;
                        errorList = log.Rollback(this.UserInfo.ID, this.ID_Language);
                        break;
                    }
            }

            return errorList;
        }

        //Subscribe

        public List<Subscriber> ViewSubscriberInformation()
        {
            return Subscriber.GetList();
        }

        public List<ErrorItem> EditSubscriberInformation(Subscriber p)
        {
            List<ErrorItem> errorList = Subscriber.ChangeInformation(p, this.UserInfo.ID, 0, this.ID_Language);

            if (errorList == null)
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " changed subscribe information.");
            else
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " cannot change subscribe information.");

            return errorList;
        }

        public List<ErrorItem> AddSubscriberInformation(Subscriber p)
        {
            List<ErrorItem> errorList = Subscriber.Insert(ref p, this.UserInfo.ID, this.ID_Language);

            if (errorList == null)
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " created new subscribe.");
            else
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " cannot create new subscribe.");

            return errorList;
        }

        public List<ErrorItem> DeleteSubscriber(Subscriber u, bool forever)
        {
            List<ErrorItem> errorList;
            if (forever)
                errorList = Subscriber.Delete(u.ID, this.UserInfo.ID, 1, this.ID_Language);
            else
                errorList = Subscriber.Delete(u.ID, this.UserInfo.ID, 0, this.ID_Language);

            if (errorList == null)
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " deleted subscriber.");
            else
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " cannot delete subscriber.");

            return errorList;
        }

        //Category

        

        public List<Category> ViewListNotAvailableParentCategories()
        {
            List<Category> lc = Category.GetListNotAvailableParentCategories();
            return lc;
        }

        public List<Category> ViewListNotAvailableSubCategories()
        {
            List<Category> lc = Category.GetListNotAvailableSubCategories();
            return lc;
        }

        public List<ErrorItem> DeleteCategory(Category p, bool forever)
        {
            List<ErrorItem> errorList;
            if (forever)
                errorList = Category.Delete(p.ID, this.UserInfo.ID, 1, this.ID_Language);
            else
                errorList = Category.Delete(p.ID, this.UserInfo.ID, 0, this.ID_Language);

            if (errorList == null)
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " deleted category.");
            else
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " cannot delete category.");

            return errorList;
        }

        public List<ErrorItem> EditCategoryInformation(Category p)
        {
            List<ErrorItem> errorList = Category.ChangeInformation(p, this.UserInfo.ID, this.ID_Language);

            if (errorList == null)
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " changed category information.");
            else
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " cannot change category information.");

            return errorList;
        }

        public List<ErrorItem> EditDetailCategoryInformation(DetailCategory dp)
        {
            List<ErrorItem> errorList = DetailCategory.ChangeInformation(dp, this.UserInfo.ID, this.ID_Language);

            if (errorList == null)
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " changed category information.");
            else
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " cannot change category information.");

            return errorList;
        }

        public List<ErrorItem> InsertCategory(ref Category p)
        {
            List<ErrorItem> errorList = Category.Insert(ref p, this.UserInfo.ID, this.ID_Language);

            if (errorList == null)
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " created new category.");
            else
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " cannot create new category.");

            return errorList;
        }

        public List<ErrorItem> InsertSubCategoryToCategory(Category c, int id_SubCategory)
        {
            List<ErrorItem> el = c.InsertSubCategory(this.UserInfo.ID, id_SubCategory, this.ID_Language);
            return el;
        }

        public List<ErrorItem> RemoveSubCategoryFromCategory(Category c, int id_SubCategory)
        {
            List<ErrorItem> el = c.RemoveSubCategory(this.UserInfo.ID, id_SubCategory, this.ID_Language);
            return el;
        }

        //User

        public List<ErrorItem> DeleteUser(User u, bool forever)
        {
            List<ErrorItem> errorList = new List<ErrorItem>();
            if (forever)
                errorList = User.Delete(u.ID, this.UserInfo.ID, 1, this.ID_Language);
            else
                errorList = User.Delete(u.ID, this.UserInfo.ID, 0, this.ID_Language);

            if (errorList == null)
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " deleted user.");
            else
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " cannot delete user.");

            return errorList;
        }

        public List<ErrorItem> EditUserInformation(User u)
        {
            List<ErrorItem> errorList = User.ChangeInformation(u, this.UserInfo.ID, this.ID_Language);

            if (errorList == null)
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " changed user information.");
            else
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " cannot change user information.");

            return errorList;
        }

        public List<ErrorItem> EditDetailUserInformation(DetailUser u)
        {
            List<ErrorItem> errorList = DetailUser.ChangeInformation(u, this.UserInfo.ID, this.ID_Language);

            if (errorList == null)
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " changed user information.");
            else
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " cannot change user information.");

            return errorList;
        }

        public List<ErrorItem> InsertUser(ref User u)
        {
            List<ErrorItem> errorList = User.Insert(ref u, this.UserInfo.ID, this.ID_Language);

            if (errorList == null)
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " created new user.");
            else
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " cannot create new user.");

            return errorList;
        }

        //Customer

        public List<ErrorItem> DeleteCustomer(Customer u, bool forever)
        {
            List<ErrorItem> errorList = new List<ErrorItem>();
            if (forever)
                errorList = Customer.Delete(u.ID, this.UserInfo.ID, 1, this.ID_Language);
            else
                errorList = Customer.Delete(u.ID, this.UserInfo.ID, 0, this.ID_Language);

            if (errorList == null)
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " deleted Customer.");
            else
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " cannot delete Customer.");

            return errorList;
        }

        public List<ErrorItem> EditUserInformation(Customer u)
        {
            List<ErrorItem> errorList = Customer.ChangeInformation(u, this.UserInfo.ID, this.ID_Language);

            if (errorList == null)
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " changed Customer information.");
            else
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " cannot change Customer information.");

            return errorList;
        }

        public List<ErrorItem> EditDetailUserInformation(DetailCustomer u)
        {
            List<ErrorItem> errorList = DetailCustomer.ChangeInformation(u, this.UserInfo.ID, this.ID_Language);

            if (errorList == null)
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " changed Customer information.");
            else
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " cannot change Customer information.");

            return errorList;
        }

        public List<ErrorItem> InsertCustomer(ref Customer u, ref DetailCustomer du, ref User i)
        {
            List<ErrorItem> errorList = Customer.Insert(ref u, ref du, ref i, this.UserInfo.ID, this.ID_Language);

            if (errorList == null)
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " created new Customer.");
            else
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " cannot create new Customer.");

            return errorList;
        }

        //Image
        public List<ErrorItem> UploadImage(Image img, int id_Category, int isPrimary)
        {
            List<ErrorItem> errorList = Image.Insert(ref img, this.UserInfo.ID, id_Category, isPrimary, this.ID_Language);

            if (errorList == null)
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " uploaded image.");
            else
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " cannot upload image.");

            return errorList;
        }

        public List<ErrorItem> EditImage(Image img, int isChangeSource)
        {
            List<ErrorItem> errorList = Image.ChangeInformation(img, this.UserInfo.ID, 0, this.ID_Language, isChangeSource);

            if (errorList == null)
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " changed image information.");
            else
                VietImageDB_Log.WriteLog(this.UserInfo.Username + " cannot change image information.");

            return errorList;
        }

        public List<Image> ViewUploadedImagesDaily(int count, int from, out int n, DateTime date)
        {
            List<Image> l = Image.GetListAvailableByUploader(this.UserInfo.ID, count, from, out n, date);
            return l;
        }

        public List<Image> ViewUploadedImagesWeekly(int count, int from, out int n, DateTime beginDate, DateTime endDate)
        {
            List<Image> l = Image.GetListAvailableByUploader(this.UserInfo.ID, count, from, out n, beginDate, endDate);
            return l;
        }

        public List<Image> ViewUploadedImagesMonthly(int count, int from, out int n, int month, int year)
        {
            List<Image> l = Image.GetListAvailableByUploader(this.UserInfo.ID, count, from, out n, month, year);
            return l;
        }

        public List<Image> ViewNotPublishImages(int id_Category, int count, int from, out int n)
        {
            List<Image> l = Image.GetListNotPublish(this.UserInfo.ID, id_Category, count, from, out n);
            return l;
        }

        public List<ErrorItem> DeleteImage(int id, int deep)
        {
            List<ErrorItem> errorList = Image.Delete(id, this.UserInfo.ID, deep, ID_Language);

            return errorList;
        }

        public List<ErrorItem> EditDetailImage(DetailImage di)
        {
            List<ErrorItem> errorList = DetailImage.ChangeInformation(di, this.UserInfo.ID, ID_Language);

            return errorList;
        }

        public List<ErrorItem> SetPrimaryImage(System.Data.Linq.Binary image, Category c)
        {
            List<ErrorItem> errorList = c.SetPrimaryImage(image, UserInfo.ID, ID_Language);

            return errorList;
        }
        #endregion

        #region Reports
        public List<MonthlyElement> MonthlyEarning(int year)
        {
            List<MonthlyElement> chart = new List<MonthlyElement>();
            for (int i = 0; i < 12; i++)
            {
                MonthlyElement e = new MonthlyElement();
                e.Month = i + 1;
                e.Fund = Payment.GetFunds(i, year);
                chart.Add(e);
            }
            return chart;
        }

        public List<DailyElement> DailyEarning(DateTime beginDate, DateTime endDate)
        {
            List<DailyElement> chart = new List<DailyElement>();
            DateTime next = beginDate;
            while (next != endDate)
            {
                DailyElement e = new DailyElement();
                e.Date = next;
                e.Fund = Payment.GetFunds(next);
                chart.Add(e);
                next = next.AddDays(1);
            }
            return chart;
        }

        public List<WeeklyElement> WeeklyEarning(int beginWeek, int endWeek, int year)
        {
            List<WeeklyElement> chart = new List<WeeklyElement>();

            for (int i = beginWeek; i <= endWeek; i++)
            {
                DateTime beginDate = new DateTime();
                DateTime endDate = new DateTime();
                Week.GetWeek(i, year, ref beginDate, ref endDate);
                WeeklyElement e = new WeeklyElement();
                e.BeginDate = beginDate;
                e.EndDate = endDate;
                e.Fund = Payment.GetFunds(beginDate, endDate);

                chart.Add(e);
            }

            return chart;
        }

        #endregion
    }
}